package com.edison.demo.until;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author edison
 */
public class OpenAcctDtGenerator {

    private OpenAcctDtGenerator(){
        throw new IllegalStateException("Utility class");
    }

    public static Date generateRandomDateBeforeToday() {
        // Define the start date range (e.g., Jan 1, 2019)
        LocalDate startDate = LocalDate.of(2019, 1, 1);
        LocalDate endDate = LocalDate.now();

        // Generate a random date within the range
        long randomEpochDay = ThreadLocalRandom.current().nextLong(startDate.toEpochDay(), endDate.toEpochDay());
        LocalDate randomLocalDate = LocalDate.ofEpochDay(randomEpochDay);

        // Convert LocalDate to java.util.Date
        return Date.from(randomLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

    }
    public static String generateFutureDate() {
        // Start from tomorrow
        LocalDate startDate = LocalDate.now().plusDays(1);
        // Define an upper bound for random future days (e.g., up to one year from today)
        LocalDate endDate = startDate.plusYears(1);

        // Generate a random date within the range
        long randomEpochDay = ThreadLocalRandom.current().nextLong(startDate.toEpochDay(), endDate.toEpochDay());
        LocalDate randomFutureDate = LocalDate.ofEpochDay(randomEpochDay);

        // Format date as "yyyyMMdd"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return randomFutureDate.format(formatter);
    }


    public static String generateRandomDateStringBeforeToday() {
        // Define the start date range (e.g., Jan 1, 1900)
        LocalDate startDate = LocalDate.of(1900, 1, 1);
        LocalDate endDate = LocalDate.now();

        // Generate a random LocalDate within the range
        long randomEpochDay = ThreadLocalRandom.current().nextLong(startDate.toEpochDay(), endDate.toEpochDay());
        LocalDate randomLocalDate = LocalDate.ofEpochDay(randomEpochDay);

        // Convert LocalDate to Date
        Date randomDate = Date.from(randomLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        // Format Date as yyyyMMdd
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        return formatter.format(randomDate);
    }

}
